package app.controllers.admin.vote;

import app.Const;
import app.models.DaoKit;
import app.models.vote.VoteCatetory;
import com.github.sog.controller.BasicController;
import com.github.sog.controller.datatables.core.DatatablesCriterias;
import com.github.sog.kit.date.DateProvider;
import com.github.sog.libs.AppFunc;
import com.github.sog.plugin.sqlinxml.SqlKit;
import com.jfinal.plugin.activerecord.Page;

import java.util.List;

/**
 * <p>
 * 问卷标题管理
 * </p>
 *
 * @author liuhui
 * @version 1.0 14-5-15 下午10:51
 */
public class TitleController extends BasicController {

    public void index(){
        render("index.ftl");
    }


    public void list() {
        final DatatablesCriterias criterias = getCriterias();
        String name = getPara("name");
        Page<VoteCatetory> page = VoteCatetory.dao.datatable(criterias,name);

        renderDataTables(page, criterias);
    }

    public void create() {
        VoteCatetory category = new VoteCatetory();
        setAttr("oper", "create");
        setAttr("category", category);
        render("item.ftl");
    }

    public void edit() {
        VoteCatetory category = VoteCatetory.dao.findById(getPara());
        setAttr("oper", "edit");
        setAttr("category", category);
        render("item.ftl");
    }

    public void save() {
        VoteCatetory category = getModel(VoteCatetory.class, "category");
        if (DaoKit.isNew(category)) {
            category.set("create_time", DateProvider.DEFAULT.getCurrentDate());
            category.set("catetory",0);
            category.save();
        } else {
            category.update();
        }
        redirect("/admin/vote/title/index");
    }

    public void remove() {
        String remove_ids = getPara(Const.DATA_PARAM);
        List<String> ids = AppFunc.COMMA_SPLITTER.splitToList(remove_ids);
        if (DaoKit.batch_remove(SqlKit.sql("votecatetory.remove"), ids)) {
            renderSuccess();
        } else {
            renderError("删除出现错误");
        }
    }
}
